package com.beiding.my.mql.utils;

import java.util.*;

public class MqlUtils {


    public static SubMqlHide hideSubMql(String o) {
        SubMqlHide pair = new SubMqlHide(o);
        pair.hide();
        return pair;
    }

    public static List<KeywordBlock> splitMql(String text) {
        KeywordGroup keywordGroup = new KeywordGroup(text, "select", "insert", "update", "delete");
        keywordGroup.find();
        return new ArrayList<>(keywordGroup.getBlockMap().values());
    }

    public static boolean isMql(String smql) {
        smql = smql.trim();
        return smql.startsWith("select ") || smql.startsWith("insert ") || smql.startsWith("delete ") || smql.startsWith("update ");
    }


    public static Map<String, KeywordBlock> makeSegmentMap(String mql, String[] keywords, String[] segmentKeywords) {

        KeywordGroup keywordGroup = new KeywordGroup(mql, keywords);
        keywordGroup.find();

        Map<String, KeywordBlock> blockMap = keywordGroup.getBlockMap();

        Map<String, KeywordBlock> r = new HashMap<>();

        for (String keyword : segmentKeywords) {
            KeywordBlock keywordBlock = blockMap.get(keyword);
            if (keywordBlock != null) {
                r.put(keyword, keywordBlock);
            }
        }

        return r;
    }


    private static final Set<Character> standardFieldAllowSet = new HashSet<>();
    private static final Set<Character> returnAllowSet = new HashSet<>();

    static {
        for (char i = 'a'; i <= 'z'; i++) {
            standardFieldAllowSet.add(i);
        }

        for (char i = 'A'; i <= 'Z'; i++) {
            standardFieldAllowSet.add(i);
        }

        for (char i = '0'; i <= '1'; i++) {
            standardFieldAllowSet.add(i);
        }
        standardFieldAllowSet.add('.');

        returnAllowSet.addAll(standardFieldAllowSet);
        returnAllowSet.add('*');
    }


    public static Set<Block> findFiled(String mql, Set<String> set) {

        StringHide hide = hideString(mql);

        //从没有字符串的结果中查找
        mql = hide.getResult();

        //所有找到的结果
        Set<Block> blocks = new HashSet<>();
        for (String s : set) {
            FindPrefix findPrefix = new FindPrefix(mql, standardFieldAllowSet, s + ".");
            findPrefix.find();
            blocks.addAll(findPrefix.getResult());
        }
        return blocks;
    }


    public static String recover(String text, Map<Pair, String> map) {

        List<Pair> pairs = new ArrayList<>(map.keySet());
        pairs.sort(Comparator.comparingInt(Pair::getStart));

        //根据开始位置进行排序处理
        int lastEnd = 0;
        StringBuilder builder = new StringBuilder();
        for (Pair pair : pairs) {
            builder.append(text, lastEnd, pair.getStart());
            builder.append(map.get(pair));
            lastEnd = pair.getEnd() + 1;
        }

        if (lastEnd < text.length()) {
            builder.append(text.substring(lastEnd));
        }
        return builder.toString();
    }

    public static StringHide hideString(String mql) {
        StringHide hide = new StringHide(mql, "'");
        hide.hide();
        hide = new StringHide(hide.getResult(), "\"");
        hide.hide();
        return hide;
    }


    public static Block findStandardReturn(String mql, Set<String> keySet) {
        StringHide hide = hideString(mql);

        //从没有字符串的结果中查找
        mql = hide.getResult();

        //所有找到的结果
        for (String s : keySet) {
            FindPrefix findPrefix = new FindPrefix(mql, returnAllowSet, s + ".");
            findPrefix.find();
            List<Block> result = findPrefix.getResult();
            if (result.size() > 0) {
                return result.get(0);
            }
        }

        return null;

    }
}
